
import { _decorator, Label } from "cc";
import { BaseUI } from "../../framework/base/BaseUI";
import { ADType } from "../../framework/config/Appcfg";
import { SDKMgr } from "../../framework/manager/SDKMgr";
import { UIMgr } from "../../framework/manager/UIMgr";
import { PlayerMgr } from "../manager/PlayerMgr";
const { ccclass, property } = _decorator;

@ccclass('UIPower')
export class UIPower extends BaseUI {

    //是否正在游戏
    isGaming: boolean = false;
    callback: Function = null;
    private type: number;
    private titleText: string = ""
    public init(param1?: any, param2?: any, param3?: any): void {
        this.type = param1
        this.titleText = param2
        this.callback = param3
    }

    start() {
        this.initBtns();
        this.initView();
    }

    initView() {
        this.findChilds("titleLab").getComponent(Label).string = this.titleText
        if (this.type == 1) {
            this.findChilds("downBtns0").active = true;
            this.findChilds("starGameBtnTipNode").active = true;

            this.findChilds("getPowerBtnNodeTip").active = false;
            this.findChilds("downBtns1").active = false
            this.findChilds("getWuXian").active = false;
            this.findChilds("powerTip1").getComponent(Label).string = `连续观看${3 - PlayerMgr.instance.userData.data.videoWatchedCount}/3次广告\n可解锁24小时无限体力`;
        } else if (this.type == 2) {
            this.findChilds("downBtns0").active = true;
            this.findChilds("getPowerBtnNodeTip").active = true;

            this.findChilds("starGameBtnTipNode").active = false;
            this.findChilds("downBtns1").active = false
            this.findChilds("getWuXian").active = false;

            this.findChilds("powerTip2").getComponent(Label).string = `连续观看${3 - PlayerMgr.instance.userData.data.videoWatchedCount}/3次广告\n可解锁24小时无限体力`
        } else if (this.type == 3) {
            this.findChilds("downBtns0").active = false;
            this.findChilds("getPowerBtnNodeTip").active = false;
            this.findChilds("starGameBtnTipNode").active = false;

            this.findChilds("downBtns1").active = true
            this.findChilds("getWuXian").active = true;
        }
    }



    initBtns() {
        this.findChilds("btnCloseView").onClick(this.closeBtn, this)
        this.findChilds("btnCloseView1").onClick(this.closeBtn, this)
        this.findChilds("btnCloseView2").onClick(this.closeBtn, this)
        this.findChilds("btnGetPower").on('click', this.onAddPower, this)
    }
    closeBtn() {
        this.closeUI();
    }
    onAddPower() {
        SDKMgr.getInstance().playVideo((isComp) => {
            if (isComp) {
                PlayerMgr.instance.incrementVideoWatched()
                PlayerMgr.instance.userData.addRoleHp(1)
                if (PlayerMgr.instance.isUnlimitedHp()) {
                    this.init(3, "提示")
                    this.initView()
                } else {
                    this.closeUI();
                    this.callback && this.callback()
                }
                UIMgr.instance.showTip("恭喜！获得体力+1")
            }
            SDKMgr.getInstance().reportEvent("showad", {
                "adtype": "video",
                "opportunity": ADType.ADDHP,
                "passpercent": 0,
                "video_complete": isComp + '',
                "is_stage": "false",
                "stage": "体力恢复",
                "level_type": "主页"
            });
        }, ADType.ADDHP)
    }

    btnCloseView() {
        this.closeUI();
    }



}
